package com.zzptc.zzsgz.bdsjws;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.nostra13.universalimageloader.utils.L;
import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zzptc.zzsgz.bdsjws.adapter.BBXAdapter;
import com.zzptc.zzsgz.bdsjws.com.Constants;
import com.zzptc.zzsgz.bdsjws.db.BBXDao;
import com.zzptc.zzsgz.bdsjws.db.DaoMaster;
import com.zzptc.zzsgz.bdsjws.db.DaoSession;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.BBX;
import com.zzptc.zzsgz.bdsjws.vo.Contact;


import org.greenrobot.greendao.database.Database;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by zzsgz on 2017/4/20.
 */

public class MyApplication extends Application {
    //功能：1、提供全局的上下文 2、初始化KLOG
    static Context context;
    static DaoSession daoSession;
    static DaoSession mobiledaoSession;
    static List<Contact> list;
    static List<Contact> sosList=new ArrayList<Contact>();
    public static List<Contact> getSosList() {

        return sosList;
    }
    public static List<Contact> getList() {
        return list;
    }
    //真正的入口方法
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        initClient();
        KLog.init(BuildConfig.LOG_DEBUG, Constants.LOG_FLAG);
        initDatabase();
        initDatabase1();
        //耗时操作 放入线程
        new Thread(new Runnable() {
            public void run() {
                initDatatoTable();
                //将外部数据库拷贝到手机存储器中
                try {
                    MyUtil.copyFileToMobile(context,"mobile.db","mobile.db");
                    list=MyUtil.getAllContacts();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void initClient() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);

    }

    public static Context getContext() {

        return context;
    }

    //初始化GreenDao    BBX
    private static void initDatabase() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(context, Constants.DATABASE_NAME);
        Database database = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(database);
        daoSession = daoMaster.newSession();
    }

    /**
     * BBX
     * @return
     */
    public static DaoSession getDaoSession() {
        return daoSession;
    }

    /**
     * 外部数据库的
     */

    private static void initDatabase1() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(context, "mobile.db");
        Database database = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(database);
        mobiledaoSession = daoMaster.newSession();
    }

    public static DaoSession getMobiledaoSession() {
        return mobiledaoSession;
    }


    //将数据新增到表 要对表进行增删改查 必须要获取BBXDao对象   只能操作一次  共享首选项
    public void initDatatoTable() {
        DaoSession daoSession = getDaoSession();
        BBXDao bbxDao = daoSession.getBBXDao();
//        try {
//            MyUtil.copyFileToMobile(context,"mobile.db","aaa.db");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        SharedPreferences sharedPreferences = MyUtil.getSharedPre();
        boolean b = sharedPreferences.getBoolean("inserttable", false);
        //控制只初始化一次
        if (!b) {
            //数据源在哪  string.xml中
            //读取 string.xml
            String[] names = getResources().getStringArray(R.array.bbx_name);
            String[] icons = getResources().getStringArray(R.array.bbx_icon);

            for (int i = 0; i < names.length; i++) {
                BBX bbx = new BBX(null, names[i], icons[i]);
                try {
                    long m = bbxDao.insert(bbx);
                    if (m > 0) {
                        KLog.i("新增成功");
                    } else {
                        KLog.i("新增失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
            int len=bbxDao.loadAll().size();
            KLog.i("=============新增了"+len+"条记录================");
            for(BBX bbx:bbxDao.loadAll()){
                KLog.i(bbx.getName()+"========================"+bbx.getIcon());
            }

            //新增完畢，將共享首选项中的值改為true
          SharedPreferences.Editor editor= sharedPreferences.edit();
            editor.putBoolean("inserttable",true);
            editor.commit();

          //  sharedPreferences.edit().putBoolean("inittable", true).commit();
        }


    }


}
